// pages/home/home.js
import { storeApi, commonApi } from '../../api/index';
// 获取app
var app = getApp();
Page({
  data: {
    pageParam: {
      page: 1,
      page_size: 10
    },
    total: 0, // 总门店数量
    showLoginTipPopup: false,
    showLoginPopup: false,
    list: [],
  },
  async getData() {
    try {
      const location = app.globalData.location
      // console.log(app.globalData.location)
      // console.log(location)
      const res = await storeApi.queryStoreList({
        ...this.data.pageParam,
        coordinate_latitude: location?.latitude ?? '',
        coordinate_longitude: location?.longitude ?? '',
      })
      this.setData({
        list: res.list,
        total: res.total,
        pageParam: {
          ...this.data.pageParam,
          page: +res.page + 1
        }
      })
    } catch (e) {

    }
  },
  async getUserInfo() {
    try {
      let res = await commonApi.getUserInfo()
      // wx.setStorageSync('userInfo', res);
      this.setData({
        userInfo: res
      })
    } catch (e) { }
  },
  onLoad: async function (options) {
    const self = this
    wx.getFuzzyLocation({
      success(res) {
        app.globalData.location = res
      },
      complete(){
        self.getData()
      }
    });

  },
  onReady: function () { },
  onShow: function () { 
     this.getUserInfo()
  },
  onShowLoginTipPopup() {
    this.setData({
      showLoginTipPopup: true,
    });
  },
  onShowLoginPopup() {
    this.setData({
      showLoginPopup: true,
    });
  },
  toBookStep(data) {
    // console.log(data)
    wx.navigateTo({
      // url: `/pages/book-step-old/book-step`,
      url: `/pages/book-step/book-step`,
    });
  },
  // 从这里开始删除 start ！！！！！
  toBookForCustomer() {
    wx.navigateTo({
      url: `/pages/book-for-customer/book-for-customer`,
    });
  },
  toNewPersonCoupon() {
    wx.navigateTo({
      url: `/pages/new-person-coupon/new-person-coupon`,
    });
  },
  toOrderPay() {
    wx.navigateTo({
      url: `/pages/order-pay/order-pay`,
    });
  },
  // 删除 end ！！！！！！！！！！
  onHide: function () { },
  onUnload: function () { },
  onReachBottom: function () {
    const { total, list } = this.data
    if (+total > list.length) {
      this.getData();
    }
  },
  onShareAppMessage: function () {
    return {
      title: '频繁染白发，就找染博士',
      imageUrl:
        'https://ranboss.oss-cn-beijing.aliyuncs.com/ranstore/share.png',
    };
  },

  onPageScroll: function (e) { },
});
